<template>
  <div>
    <el-container style="border: 18px solid #eee">
      <el-header style="text-align: right; font-size: 12px" height="20px">
        <el-input
          v-model="kwInput"
          size="mini"
          placeholder="输入名称或者page id"
          style="width: 40%;float: right"
          clearable
          class=""
          @keyup.native.enter="handleSearch"
        >
          <el-button
            slot="append"
            size="mini"
            style="width: 180px"
            type="success"
            icon="el-icon-search"
            @click="handleSearch"
          >搜索
          </el-button>
        </el-input>
        <div class="btn-group" style="float: right">
          <el-button size="mini" type="primary" @click="handleAction('create')">添加</el-button>
        </div>
      </el-header>
      <el-main>
        <el-table
          v-loading.body="listLoading"
          :data="tableData"
          border
          style="width: 100%"
        >
          <el-table-column
            prop="page_id"
            label="page_id"
            width="150px"
          >
            <template slot-scope="scope">
              <el-link :href="'https://www.facebook.com/' + scope.row.page_id">{{ scope.row.page_id }}</el-link>
            </template>
          </el-table-column>
          <el-table-column
            prop="name"
            label="name"
          />
          <el-table-column
            prop="title"
            label="title"
          />
          <el-table-column
            prop="likes"
            label="likes"
            width="90px"
          />
          <el-table-column
            prop="followers"
            label="followers"
            width="90px"
          />
          <el-table-column
            prop="active_ads"
            label="running"
            width="90px"
          />
          <el-table-column
            prop="total_ads"
            label="total"
            width="90px"
          />
          <el-table-column
            label="监控"
            width="100"
          >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.isActive"
                active-color="#13ce66"
                inactive-color="#ff4949"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="广告"
            width="100"
          >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.ad_running"
                active-color="#13ce66"
                inactive-color="#ff4949"
              />
            </template>
          </el-table-column>
          <el-table-column
            prop="page_create_at"
            label="页面创建日期"
          />
          <el-table-column
            prop="update_at"
            label="最后抓取"
          />
          <el-table-column align="center" label="操作" width="200">
            <template slot-scope="scope">
              <el-button size="mini" type="success" @click="handleAction('edit', scope.row)">修改</el-button>
              <el-button size="mini" type="danger" @click="handleAction('delete', scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-pagination
        v-if="tableData.length"
        background
        class="mt-pagination"
        :current-page="page"
        :page-sizes="[10,50,100,200,300,500]"
        :page-size="pagesize"
        layout="total"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      />
    </el-container>
    <el-dialog
      :title="title"
      :visible.sync="innerDialogVisible"
      width="60%"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item v-if="action !== 'delete'" label="page_id">
          <el-input v-model="form.page_id" />
        </el-form-item>
        <el-form-item v-if="action !== 'delete'" label="是否监控">
          <el-switch
            v-model="form.isActive"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="监控"
            inactive-text="不监控"
          >
            >
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="innerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Get, Post, Put, Delete } from '@/api/facebook/facebookpages'
import qs from 'qs'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}

export default {
  data() {
    return {
      action: '',
      listLoading: false,
      kwInput: '',
      order_by: '',
      order_desc: '',
      // pagination
      pagesize: 10,
      page: 1,
      total: 0,
      tableData: [],
      form: {
        id: '',
        page_id: '',
        isActive: true
      },
      innerDialogVisible: false,
      title: '添加cookies'
    }
  },
  created() {
    this.getList()
  },
  methods: {
    resetTemp() {
      this.form = {
        page_id: ''
      }
    },
    onSubmit() {
      Post(this.form).then((response) => {
        this.form.page_id = ''
        this.innerDialogVisible = false
        this.getList()
      }).catch(error => {
        console.log(error)
      })
    },
    getList() {
      this.listLoading = true
      Get(null,
        qs.stringify({
          kw: this.kwInput,
          pagesize: this.pagesize,
          page: this.page,
          order_by: this.order_by,
          order_desc: this.order_desc
        })).then((response) => {
        this.tableData = response.data.data
        this.total = response.data.total
        this.listLoading = false
      }).catch(error => {
        console.log(error)
        this.listLoading = false
      })
    },
    handleAction(action, row) {
      this.action = action
      switch (action) {
        case 'create':
          this.width = '50%'
          this.title = row
          this.resetTemp()
          this.innerDialogVisible = true
          break
        case 'delete':
          this.width = '30%'
          this.title = `确认要删除${row.account}?`
          this.form = Object.assign({}, row)
          this.innerDialogVisible = true
          break
        case 'edit':
          this.width = '50%'
          this.title = `修改账号${row.account}`
          this.form.id = row.id
          this.form.page_id = row.page_id
          this.form.isActive = row.isActive
          this.form.ad_running = row.ad_running
          console.log(this.form, row)
          this.innerDialogVisible = true
          break
      }
    },
    confirm() {
      const _data = this.form
      switch (this.action) {
        case 'create':
          Post(this.form
          ).then(response => {
            this.innerDialogVisible = false
            this.getList()
          })
          break
        case
          'delete'
          :
          Delete(_data.id).then((response) => {
            this.innerDialogVisible = false
            this.getList()
          })
          break
        case
          'edit'
          :
          Put(_data.id, {
            account: _data.account,
            platform: _data.platform,
            cookie_json: _data.cookie_json,
            effective: _data.effective
          }).then((response) => {
            this.innerDialogVisible = false
            this.$emit('reload')
            this.getList()
            this.notify('更新成功')
          })
          break
      }
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.getList()
    },
    handleSearch() {
      this.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.getList()
    }
  }

}
</script>

<style scoped>

</style>
